package com.ledim.widget.loadmore;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.letv.android.young.client.R;

public class LoadMoreDefaultFooterView extends RelativeLayout
        implements LoadMoreUIHandler, LoadMoreStateCode {

    private TextView mTextView;

    public LoadMoreDefaultFooterView(Context context) {
        this(context, null);
    }

    public LoadMoreDefaultFooterView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadMoreDefaultFooterView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setupViews();
    }

    private void setupViews() {
        LayoutInflater.from(getContext())
                      .inflate(R.layout.cube_views_load_more_default_footer, this);
        mTextView = (TextView) findViewById(R.id.cube_views_load_more_default_footer_text_view);
    }

    @Override
    public void onLoading(LoadMoreContainer container) {
        setVisibility(VISIBLE);
        mTextView.setText(R.string.cube_views_load_more_loading);
    }

    /**
     * @param container
     * @param errorCode
     *         1表示刷新，其他都是加载的处理、0表示正常、-1表示unkonw错误、-2表示网络异常、-3表示服务器异常
     * @param hasMore
     */
    @Override
    public void onLoadFinish(LoadMoreContainer container, int errorCode, boolean hasMore) {
        if (errorCode == STATE_REFRESH) { // 刷新列表把footer隐藏
            setVisibility(GONE);
        } else if (errorCode == STATE_NORMAL) {
            setVisibility(VISIBLE);
            if (!hasMore) {
                mTextView.setText(R.string.cube_views_load_more_loaded_no_more);
            } else {
                mTextView.setText(R.string.cube_views_load_more_loading);
            }
        } else if (errorCode == ERROR_UNKONW) {
            setVisibility(VISIBLE);
            mTextView.setText(R.string.cube_views_load_more_error_unkonw);
        } else if (errorCode == ERROR_NET) {
            setVisibility(VISIBLE);
            mTextView.setText(R.string.cube_views_load_more_error_net);
        } else if (errorCode == ERROR_SE) {
            setVisibility(VISIBLE);
            mTextView.setText(R.string.cube_views_load_more_error_se);
        }

    }

    @Override
    public void onWaitToLoadMore(LoadMoreContainer container) {
        setVisibility(VISIBLE);
        mTextView.setText(R.string.cube_views_load_more_click_to_load_more);
    }
}
